options compress=YES;

libname original ''; 
libname fra '';

*****************************************************************;
* COLLAPSE BY COHORT AND RETIREMENT/CLAIMING MONTH-YEAR AND MERGE;
* ***************************************************************;

***************************************************************************;
* TWO SETS OF FILES: RESTRICT TO 10+ YEARS OF EARNINGS, THOSE WORKING AT 55;
***************************************************************************;

***********************************************************;
* APPEND ALL SEGMENTS ;
* *********************************************************;

data mbrphusser_all;
	set 
		fra.mbrphusser02
		fra.mbrphusser03
		fra.mbrphusser05
		fra.mbrphusser06
		fra.mbrphusser09
		fra.mbrphusser10
		fra.mbrphusser15
		fra.mbrphusser16
		fra.mbrphusser19
		fra.mbrphusser20
		;
run;


************************************************************************;
* SIMULATED "MONTH AGE TRICK", ACTUAL MONTH AGE (CLAIMING ONLY);
************************************************************************;

* MONTH AGE TRICK FOR LABOR FORCE EXIT AND CLAIMING; 
data mbrphusser_earn10yr;
	set mbrphusser_all (where=(n_earnyrnum60>=10));
run;

data mbrphusser_earn55;
	set mbrphusser_all (where=(n_earnat55=1));
run;

%macro collapse(set);
* Retirement flow: reshape long and collapse ;
data collapse_lfexitflow_&set.;
	set mbrphusser_&set. (keep= ssn ser_dobyy ser_dobmm n_rflow_:) ;

	array	arflow(1951:2013)	n_rflow_1951-n_rflow_2013;
	
	do year = 1951 to 2013;
		n_rflow = arflow(year);
		output;
	end;

	drop 	
			n_rflow_1951-n_rflow_2013 
			;	
run;

data collapse_lfexitflow_&set.;
	set collapse_lfexitflow_&set. (where=(n_rflow=1));
run;

proc means noprint data=collapse_lfexitflow_&set. nway;
	class ser_dobyy ser_dobmm year;
	var n_rflow;
	output out=collapses_lfexitflow_&set.
	N(n_rflow)=&set._lfexit_flow;
run;

* Single retirement year ;
proc means noprint data=mbrphusser_&set. nway;
	class ser_dobyy ser_dobmm n_retire_yr;
	var ser_dob;
	output out=collapses_lfexitone_&set.
	N(ser_dob)=&set._lfexit_one;
run;

* PHUS claiming year ;
proc means noprint data=mbrphusser_&set. nway;
	class ser_dobyy ser_dobmm n_phus1old_yr;
	var ser_dob;
	output out=collapses_phusold_&set.
	N(ser_dob)=&set._claim_phusold;
run;

proc means noprint data=mbrphusser_&set. nway;
	class ser_dobyy ser_dobmm n_phus1any_yr;
	var ser_dob;
	output out=collapses_phusany_&set.
	N(ser_dob)=&set._claim_phusany;
run;

* MBA claiming year ;
proc means noprint data=mbrphusser_&set. nway;
	class ser_dobyy ser_dobmm n_mba1old_yr;
	var ser_dob;
	output out=collapses_mbaold_&set.
	N(ser_dob)=&set._claim_mbaold;
run;

proc means noprint data=mbrphusser_&set. nway;
	class ser_dobyy ser_dobmm n_mba1any_yr;
	var ser_dob;
	output out=collapses_mbaany_&set.
	N(ser_dob)=&set._claim_mbaany;
run;


* ACTUAL MONTH AGE (CLAIMING ONLY);

* PHUS claiming year ;
proc means noprint data=mbrphusser_&set. nway;
	class ser_dobyy ser_dobmm n_phus1old_yr n_phus1old_mo;
	var ser_dob;
	output out=collapsea_phusold_&set.
	N(ser_dob)=&set._claim_phusold;
run;

proc means noprint data=mbrphusser_&set. nway;
	class ser_dobyy ser_dobmm n_phus1any_yr n_phus1any_mo;
	var ser_dob;
	output out=collapsea_phusany_&set.
	N(ser_dob)=&set._claim_phusany;
run;

* MBA claiming year ;
proc means noprint data=mbrphusser_&set. nway;
	class ser_dobyy ser_dobmm n_mba1old_yr n_mba1old_mo;
	var ser_dob;
	output out=collapsea_mbaold_&set.
	N(ser_dob)=&set._claim_mbaold;
run;

proc means noprint data=mbrphusser_&set. nway;
	class ser_dobyy ser_dobmm n_mba1any_yr n_mba1any_mo;
	var ser_dob;
	output out=collapsea_mbaany_&set.
	N(ser_dob)=&set._claim_mbaany;
run;
%mend collapse;

%collapse(earn10yr);
%collapse(earn55);

* Merge all together;
data fra.collapsemsim_combine;

merge 
	
	collapses_lfexitflow_earn10yr
	(keep=ser_dobyy ser_dobmm year earn10yr_lfexit_flow)

	collapses_lfexitone_earn10yr
	(keep=ser_dobyy ser_dobmm n_retire_yr earn10yr_lfexit_one
	rename=(n_retire_yr=year))

	collapses_phusold_earn10yr
	(keep=ser_dobyy ser_dobmm n_phus1old_yr earn10yr_claim_phusold
	rename=(n_phus1old_yr=year))

	collapses_phusany_earn10yr
	(keep=ser_dobyy ser_dobmm n_phus1any_yr earn10yr_claim_phusany
	rename=(n_phus1any_yr=year))

	collapses_mbaold_earn10yr
	(keep=ser_dobyy ser_dobmm n_mba1old_yr earn10yr_claim_mbaold
	rename=(n_mba1old_yr=year))

	collapses_mbaany_earn10yr
	(keep=ser_dobyy ser_dobmm n_mba1any_yr earn10yr_claim_mbaany
	rename=(n_mba1any_yr=year))

	collapses_lfexitflow_earn55
	(keep=ser_dobyy ser_dobmm year earn55_lfexit_flow)

	collapses_lfexitone_earn55
	(keep=ser_dobyy ser_dobmm n_retire_yr earn55_lfexit_one
	rename=(n_retire_yr=year))

	collapses_phusold_earn55
	(keep=ser_dobyy ser_dobmm n_phus1old_yr earn55_claim_phusold
	rename=(n_phus1old_yr=year))

	collapses_phusany_earn55
	(keep=ser_dobyy ser_dobmm n_phus1any_yr earn55_claim_phusany
	rename=(n_phus1any_yr=year))

	collapses_mbaold_earn55
	(keep=ser_dobyy ser_dobmm n_mba1old_yr earn55_claim_mbaold
	rename=(n_mba1old_yr=year))

	collapses_mbaany_earn55
	(keep=ser_dobyy ser_dobmm n_mba1any_yr earn55_claim_mbaany
	rename=(n_mba1any_yr=year))
	;

	by ser_dobyy ser_dobmm year;
run;

data fra.collapsemact_combine;

merge 
	
	collapsea_phusold_earn10yr
	(keep=ser_dobyy ser_dobmm n_phus1old_yr n_phus1old_mo earn10yr_claim_phusold
	rename=(n_phus1old_yr=year n_phus1old_mo=month))

	collapsea_phusany_earn10yr
	(keep=ser_dobyy ser_dobmm n_phus1any_yr n_phus1any_mo earn10yr_claim_phusany
	rename=(n_phus1any_yr=year n_phus1any_mo=month))

	collapsea_mbaold_earn10yr
	(keep=ser_dobyy ser_dobmm n_mba1old_yr n_mba1old_mo earn10yr_claim_mbaold
	rename=(n_mba1old_yr=year n_mba1old_mo=month))

	collapsea_mbaany_earn10yr
	(keep=ser_dobyy ser_dobmm n_mba1any_yr n_mba1any_mo earn10yr_claim_mbaany
	rename=(n_mba1any_yr=year n_mba1any_mo=month))

	collapsea_phusold_earn55
	(keep=ser_dobyy ser_dobmm n_phus1old_yr n_phus1old_mo earn55_claim_phusold
	rename=(n_phus1old_yr=year n_phus1old_mo=month))

	collapsea_phusany_earn55
	(keep=ser_dobyy ser_dobmm n_phus1any_yr n_phus1any_mo earn55_claim_phusany
	rename=(n_phus1any_yr=year n_phus1any_mo=month))

	collapsea_mbaold_earn55
	(keep=ser_dobyy ser_dobmm n_mba1old_yr n_mba1old_mo earn55_claim_mbaold
	rename=(n_mba1old_yr=year n_mba1old_mo=month))

	collapsea_mbaany_earn55
	(keep=ser_dobyy ser_dobmm n_mba1any_yr n_mba1any_mo earn55_claim_mbaany
	rename=(n_mba1any_yr=year n_mba1any_mo=month))
	;

	by ser_dobyy ser_dobmm year month;
run;

* CONVERT DATA SETS TO STATA;
%let DIR=;
proc export data=fra.collapsemsim_combine
     file="&DIR\collapsemsim_combine.dta"
     dbms=DTA REPLACE;
run;
proc export data=fra.collapsemact_combine
     file="&DIR\collapsemact_combine.dta"
     dbms=DTA REPLACE;
run;


quit;


